<?php
/**
 * Created by PhpStorm.
 * User: sunfan
 * Date: 2017/8/16
 * Time: 10:00
 */

namespace App\Controller;


class MemberController extends ApiBaseController
{
    //我的个人中心
    public function info(){
        $data = $this->data;
        $id = $this->getUserID($data['token']);
        try{
            $code = 2;
            $msg = 'token无效或已过期';
            if($id){
                $info = M('Member')->where(array('id'=>$id))->field('user_name,license_name,avatar')->find();
                $info['pay_order'] = M("order")->where(array("user_id"=>$id,'status'=>1))->count();
                $info['shipped'] = M("order")->where(array("user_id"=>$id,'status'=>2))->count();
                $info['received'] = M("order")->where(array("user_id"=>$id,'status'=>3))->count();
                $where['user_id'] = $id;
                $where['status'] = array('egt',6);
                $info['refund'] = M("order")->where($where)->count();
                $code = 1;
                $msg = "成功";
            }
        }catch (\Exception $e){
            $code = -1;
            $msg = "系统错误";
        }
        $this->ApiReturn($code,$msg,$info);
    }



    //退出登录
//    public function login_out(){
//
//    }

    //全部订单
    public function all_order(){
        $data = $this->data;
        $id = $this->getUserID($data['token']);
        try{
            $code = 2;
            $msg = 'token无效或已过期';
            if($id){
                $where['o.user_id'] = $id;
                $where['o.status'] = array('neq',-1);
                $info = M("order")->alias('o')
                    ->join('cai_member m on o.user_id = m.id')
                    ->where($where)
                    ->field('o.id order_id,o.order_num,o.status,o.money')
                    ->select();
                foreach($info as $k=>$v){
                    $info[$k]['count'] = M('order_detail')->where(array('order_id'=>$v['order_id']))->count();
                    $info[$k]['pro'] = M('order_detail')->alias('d')->join('cai_product p on d.product_id = p.id')->field('p.image')->where(array('d.order_id'=>$v['order_id']))->select();
                }
                $code = 1;
                $msg = '成功';
            }

        }catch(\Exception $e){
            $code = -1;
            $msg = "系统错误";
        }
        $this->ApiReturn($code,$msg,$info);
    }

    //删除订单
    public function del_order(){
        $data = $this->data;
        $order_id = $data['order_id'];
        try{
            $code = -1;
            $msg = '缺少参数 order_id';
            if($order_id){
                $info = M('order')->where(array('id'=>$order_id))->save(array('status'=>-1));
                $code = 1;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg);
    }
    //取消订单
    public function cancel_order(){
        $data = $this->data;
        $order_id = $data['order_id'];
        try{
            $code = -1;
            $msg = '缺少参数 order_id';
            if($order_id){
                $info = M('order')->where(array('id'=>$order_id))->save(array('status'=>5));
                $code = 1;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg);
    }
    //确认收货
    public function receipt_order(){
        $data = $this->data;
        $order_id = $data['order_id'];
        try{
            $code = -1;
            $msg = '缺少参数 order_id';
            if($order_id){
                $info = M('order')->where(array('id'=>$order_id))->save(array('status'=>4));
                $code = 1;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg);
    }

    //订单详情
    public function order_detail(){
        $data = $this->data;
        $order_id = $data['order_id'];
        try{
            $code = -1;
            $msg = '缺少参数 order_id';
            if($order_id){
                $info = M('order')->alias('o')->join('cai_user_address a on o.user_id = a.user_id')
                    ->field('o.id order_id ,o.order_num,o.money,o.pro_money,o.create_time,o.status,o.pay_type,a.name,a.area,a.address,o.delivery_time')
                    ->where(array('o.id'=>$order_id))
                    ->find();
                $info['pro'] = M("order_detail")->alias('d')
                    ->join('cai_product p on d.product_id = p.id')
                    ->field('d.product_price,d.product_num,p.image,p.title,p.desc,p.id,p.attrs_key,p.attrs_value,d.product_attr')
                    ->where(array('d.order_id'=>$order_id))
                    ->select();
                $parameters = array();
                foreach($info['pro'] as $k =>$v){
                    $info['pro'][$k]['total_money'] = $v['product_price']*$v['product_num'];
                    $key = explode(',',$v[attrs_key]);
                    $value = explode('/',$v[product_attr]);
//                    dump($value);die;
                    foreach($key as $kk => $kv){
                        $parameters[$kk]['key'] = $kv;
                        $parameters[$kk]['val'] = $value[$kk];
//                        dump($parameters);
                    }
                    $info['pro'][$k]['attr'] = $parameters;
                    unset($info['pro'][$k]['product_attr']);
                    unset($info['pro'][$k]['attrs_key']);
                    unset($info['pro'][$k]['attrs_value']);
                }
                $code = 1;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg,$info);
    }

    //申请退款
    public function refund_order(){
        $data = $this->data;
        $order_id = $data['order_id'];
        try{
            $code = -1;
            $msg = '缺少参数 order_id';
            if($order_id){
                $info = M('order')->alias('o')
                    ->join('cai_member m on o.user_id = m.id')
                    ->field('o.id order_id,o.order_num,m.user_name,m.phone')
                    ->where(array('o.id'=>$order_id))
                    ->find();
                $code = 1;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg,$info);
    }

    //退款申请提交
    public function add_refund_order(){
        $data = $this->data;
        $order_id = $data['order_id'];
        $resond = $data['resond'];//退款理由
        try{
            $code = -1;
            $msg = '缺少参数';
            if($order_id && $resond){
                $info = M('order')->where(array('id'=>$order_id))->save(array('status'=>6,'resond'=>$resond));
                $code = 1;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg);
    }
    //收货地址
    public function user_address(){
        $data = $this->data;
        $user_id = $this->getUserID($data['token']);
        try{
            $code = 2;
            $msg = 'token无效或已过期';
            if($user_id){
                $info = M('Member')->alias('m')
                    ->join('cai_user_address a on m.id = a.user_id')
                    ->where(array('m.id'=>$user_id,'a.status'=>1))
                    ->field('a.id,a.user_id,a.name,a.phone,a.area,a.address,is_default')
                    ->select();
                $code = 1;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg,$info);
    }
    //设置默认地址
    public function moren_address(){
        $data = $this->data;
        $id = $data['address_id'];    //收货地址ID
        $ad['user_id'] = $this->getUserID($data['token']);
        try{
            $code = 2;
            $msg= 'token无效或已过期';
            if($ad['user_id']){
                $code = -1;
                $msg = '所需参数不能为空';
                $mss = M("user_address")->where(array('user_id'=>$ad['user_id'],'status'=>1))->save(array('is_default'=>0));
                if($id){
                    $info = M('user_address')->where(array('id'=>$id,'status'=>1))->save(array('is_default'=>1));
                    $code = 1;
                    $msg = '成功';
                }
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg);
    }
    //新增地址
    public function add_user_address(){
        $data = $this->data;
        $ad['user_id'] = $this->getUserID($data['token']);
        $ad['name'] = $data['name'];
        $ad['phone'] = $data['phone'];
        $ad['area'] = $data['area'];
        $ad['address'] = $data['address'];
        try{
            $code = 2;
            $msg = 'token无效或已过期';
            if($ad['user_id']){
                $code = -1;
                $msg = '所传参数不存在';
                if($ad['name'] && $ad['phone'] && $ad['area'] && $ad['address']){
                    $info = M('user_address')->add($ad);
                    $code = 1;
                    $msg = '成功';
                }
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg);
    }
    //删除地址
    public function del_user_address(){
        $data = $this->data;
        $id = $data['address_id'];    //收货地址ID
        try{
            $code = -1;
            $msg = '所需参数不能为空';
            if($id){
                $info = M('user_address')->where(array('id'=>$id))->save(array('status'=>2));
                $code = 1;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg);
    }

    //编辑地址
    public function edit_user_address(){
        $data = $this->data;
        $id = $data['address_id'];    //收货地址ID
        try{
            $code = -1;
            $msg = '所需参数不能为空';
            if($id){
                $info = M('user_address')->where(array('id'=>$id,'status'=>1))->field('name,phone,area,address')->find();
                $code = 1;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg,$info);
    }

    //保存编辑地址
    public function edit_post_address(){
        $data = $this->data;
        $id = $data['address_id'];     //收货地址ID
        $ad['name'] = $data['name'];
        $ad['phone'] = $data['phone'];
        $ad['area'] = $data['area'];
        $ad['address'] = $data['address'];
        try{
            $code = -1;
            $msg = '所传参数不存在';
            if($ad['name'] && $ad['phone'] && $ad['area'] && $ad['address'] && $id){
                $info = M('user_address')->where(array('id'=>$id,'status'=>1))->save($ad);
                $code = 1;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg);
    }

    //我的信息
    public function user_info(){
        $data = $this->data;
        $id = $this->getUserID($data['token']);
        try{
            $code = 2;
            $msg = 'token无效或已过期';
            if($id){
               $info = M("member")->where(array('id'=>$id))->field('user_name,license_name,license_id,license_img,phone,number,license_address,delivery_time,other')->find();
                $code = 1;
                $msg = '成功';
            }

        }catch(\Exception $e){
            $code = -1;
            $msg = "系统错误";
        }
        $this->ApiReturn($code,$msg,$info);
    }
    //注册
    public function reginster(){
        $data = $this->data;
        $create_time = date("Y-m-d H:i:s");
        $delivery_time = $data['delivery_time'];
        $password = $data['password'];
        $user_name = $data['username'];
        $license_id = $data['license_id'];
        $license_name = $data['license_name'];
        $license_img = $data['license_img'];
        $phone = $data['phone'];
        $number = $data['number'];
        $other = $data['other'];
        $license_address = $data['license_address'];
        try{
            $code = -1;
            $msg = '所需参数不能为空';
            if($delivery_time &&  $user_name && $license_id && $license_name && $license_img &&  $phone && $number && $license_address && $password){
                $info = M('member')
                ->add(array('license_address'=>$license_address,'number'=>$number,'phone'=>$phone,'license_img'=>$license_img,'license_name'=>$license_name,'license_id'=>$license_id,'user_name'=>$user_name,'delivery_time'=>$delivery_time,'create_time'=>$create_time,'password'=>md5($password)));
                $code = 0;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg);
    }

    // 生成图片验证码接口
    public function vCode($num = 4, $size = 20, $width = 0, $height = 0) {
        $data = $this->data;
        $phone = $data['phone'];
        !$width && $width = $num * $size * 4 / 5 + 5;
        !$height && $height = $size + 10;
        // 去掉了 0 1 O l 等
        $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";
        $code = '';
        for ($i = 0; $i < $num; $i++) {
            $code .= $str[mt_rand(0, strlen($str)-1)];
        }
        // 画图像
        $im = imagecreatetruecolor($width, $height);
        // 定义要用到的颜色
        $back_color = imagecolorallocate($im, 235, 236, 237);
        $boer_color = imagecolorallocate($im, 118, 151, 199);
        $text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
        // 画背景
        imagefilledrectangle($im, 0, 0, $width, $height, $back_color);
        // 画边框
        imagerectangle($im, 0, 0, $width-1, $height-1, $boer_color);
        // 画干扰线
        for($i = 0;$i < 5;$i++) {
            $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagearc($im, mt_rand(- $width, $width), mt_rand(- $height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color);
        }
        // 画干扰点
        for($i = 0;$i < 50;$i++) {
            $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color);
        }
        // 画验证码
        @imagefttext($im, $size , 0, 5, $size + 3, $text_color, 'c:\\WINDOWS\\Fonts\\simsun.ttc', $code);

//        // 验证码存到session ;
        s('IMAGE_CODE_SEEDS'.$phone,$code);
//        // 输出图像
        $dir  = './data/upload/images/';
        $file = time().mt_rand(100,999).".png";
        if(!is_dir($dir)) mkdir($dir,0777,true);
        imagepng($im,$dir.$file);

        $str = '/data/upload/images/'.$file;
        s('IMG_CODE'.$phone,$dir.$file);
        $this->ApiReturn(0,'成功',$str);

    }

    //  校验图片验证码
    public function checkImgCode(){
        $data = $this->data;
        $code = $data['code'];
        $phone = $data['phone'];
        try{
            $seeds = s('IMAGE_CODE_SEEDS'.$phone)?s('IMAGE_CODE_SEEDS'.$phone):'-';
            if(strtoupper($seeds) == strtoupper($code)){
                $codes = 0;
                $msg = '成功';
                $file = s('IMG_CODE'.$phone);
                unlink($file);
                //发送短信（待完成）
            }else{
                $codes = -1;
                $msg = '验证码错误:'.$seeds;
            }
        }catch(\Exception $e){
            $codes = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($codes,$msg);

    }

    //平台简介
    public function introduction(){
        try{
            $info = M('company')->where('id=1')->field('desc')->find();
            $code = 0;
            $msg = '成功';
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg,$info);
    }
    //常见问题
    public function problem(){
        try{
            $info = M('problem')->select();
            $code = 0;
            $msg = '成功';
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg,$info);
    }
    //加盟细则
    public function rules(){
        try{
            $info = M('company')->where('id=1')->field('rules')->find();
            $code = 0;
            $msg = '成功';
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg,$info);
    }
    //客服中心
    public function service(){
        try{
            $info = M('company')->where('id=1')->field('service')->find();
            $code = 0;
            $msg = '成功';
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg,$info);
    }
    //添加留言
    public function add_service(){
        $data = $this->data;
        $ad['user_name'] = $data['user_name'];
        $ad['phone'] = $data['phone'];
        $ad['content'] = $data['content'];
        try{
            $code = -1;
            $msg = '参数缺数';
            if($ad){
                $info = M('message')->add();
                $code = 0;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg,$info);
    }

    //收货时间
    public function get_time()
    {
        try{
            $where['status'] = 1;
            $where['type'] = 1;//常规
            $info['routine'] = M('time')->where($where)->select();
            $where['type'] = 2;//其他
            $info['Other'] = M('time')->where($where)->select();
            $code = 0;
            $msg = '成功';
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg,$info);
    }

    //上传图片
    public function upImg()
    {
        $data = $this->data;
        $base64Img = $data['upImg'];
        $info = base64_upload($base64Img);
        $this->ApiReturn($info['code'],$info['msg'],array('imgUrl'=>$info['url']));
    }

    //获取订单列表
    public function get_order_list()
    {
        $data = $this->data;
        $id = $this->getUserID($data['token']);
        try{
            $code = 2;
            $msg = 'token无效或已过期';
            if($id){
                switch ($data['status']) {
                    case 1:$where['o.status'] = array('eq',1);break;
                    case 2:$where['o.status'] = array('eq',2);break;
                    case 3:$where['o.status'] = array('eq',3);break;
                    case 6:$where['o.status'] = array('egt',6);break;
                    default:$where['o.status']='';break;
                }
                $msg = '缺少参数 status';
                if (!empty($where['o.status'])) {
                    $where['o.user_id'] = $id;
                    $info = M("order")->alias('o')
                        ->join('cai_member m on o.user_id = m.id')
                        ->where($where)
                        ->field('o.id order_id,o.order_num,o.status,o.money')
                        ->select();
                    foreach($info as $k=>$v){
                        $info[$k]['count'] = M('order_detail')->where(array('order_id'=>$v['order_id']))->count();
                        $info[$k]['pro'] = M('order_detail')->alias('d')->join('cai_product p on d.product_id = p.id')->field('p.image')->where(array('d.order_id'=>$v['order_id']))->select();
                    }
                    $code = 1;
                    $msg = '成功';
                }
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = "系统错误";
        }
        $this->ApiReturn($code,$msg,$info);


    }

    //再来一单
    public function again_order()
    {
        $data = $this->data;
        $id = $this->getUserID($data['token']);
        $order_id = $data['order_id'];
        try{
            $code = 2;
            $msg = 'token无效或已过期';
            if($id){
                $where['order_id'] = array('eq',$order_id);
                $order_detail = M('order_detail')->alias('od')
                    ->join("LEFT JOIN cai_product p ON od.product_id = p.id")
                    ->where($where)
                    ->field('product_id,product_num,product_attr,p.title,p.stock,p.price,p.price_plus')
                    ->select();
                if ($order_detail != false) {
                    $msg = '';
                    foreach ($order_detail as $key=>$item) {
                        $times = time();
                        $promotion_id = M('promotion_product')
                            ->join('LEFT JOIN cai_promotion ON cai_promotion_product.promotion_id = cai_promotion.id')
                            ->where('product_id='.$item['product_id'].' and start_time <='.$times.' and end_time > '.$times)
                            ->getField('id');
                        if (!empty($item['product_attr'])) {
                            $param = D("Parameter")->where(array('attr_value'=>$item['product_attr'],'product_id'=>$item['product_id']))->find();
                            if($param['stock'] < $item['product_num']){
                                $msg .= $item['title'].$param['attr_value'].'库存不足,';
                                continue;
                            }
                            $attr_id = $param['id'];
                        }else{
                            if($item['stock'] < $item['product_num']) {
                                $msg .= $item['title'] . '库存不足,';
                                continue;
                            }
                            $attr_id = 0;
                        }
                        $cardAddAry = array(
                            'product_id'=>$item['product_id'],
                            'product_sum'=>$item['product_num'],
                            'user_id'=>$id,
                            'attr_id'=>$attr_id,
                            'promotion_id'=>$promotion_id!=false?$promotion_id:0
                        );
                        $cart = D("Cart")->where(array('user_id'=>$id,'product_id'=>$cardAddAry['product_id'],'attr_id'=>$cardAddAry['attr_id']))->find();
                        if($cart){
                            $add = D("Cart")->where(array('id'=>$cart['id']))->setInc('product_sum',$cardAddAry['product_sum']);
                            if($add){
                                $id = $cart['id'];
                            }
                        }else{
                            $id = D("Cart")->add($cardAddAry);
                        }
                        $ids[] = $id;
                    }
                    if($ids != false){
                        $code = 1;
                        if($msg != ''){
                            $msg = trim($msg,',');
                        }else{
                            $msg = '成功';
                        }
                    }else{
                        $msg = '订单中的商品都买光了';
                    }
                }
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = "系统错误";
        }
        $this->ApiReturn($code,$msg,array('ids'=>$ids));
    }


    //获取用户是否审核成功
    public function get_member_status()
    {
        $data = $this->data;
        $id = $this->getUserID($data['token']);
        try{
            $code = 2;
            $msg = 'token无效或已过期';
            if($id){
                $info = M('member')->where('id = %d',$id)->getField('status');
                $code = 1;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = "系统错误";
        }

        $this->ApiReturn($code,$msg,$info);
    }

    //手机号登录
    public function phone_login()
    {
        $data = $this->data;
        try{
            $code = 2;
            $msg = "手机号或密码错误";
            if ($data['phone'] && $data['password']) {
                $where = array(
                    'phone'=>$data['phone'],
                    'password'=>md5($data['password']),
                    'status'=>array('lt',4)
                );
                $info = M('member')->where('phone = '.$data['phone'])->field('id,phone,password,status')->find();
                if ($info != false) {
                    $token = md5($data['phone'].time());
                    S($token,$info['id']);
                    $code = 1;
                    $msg = "成功";
                }
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = "系统错误";
        }
        $this->ApiReturn($code,$msg,array('token'=>$token));
    }

    //撤销申请
    public function Revoke_Apply()
    {

    }

    //再次编辑
    public function again_edit()
    {
//        echo $this->getToken('oxPrjlkjdd1223lkj');die;
        $data = $this->data;
        $id = $this->getUserID($data['token']);
        try{
            $code = 2;
            $msg = 'token无效或已过期';
            if($id){
                $info = M('member')->where('id = %d and status = 3',$id)->field('id,sex,openID,avatar,create_time,status',true)->find();
                $code = 1;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = "系统错误";
        }

        $this->ApiReturn($code,$msg,$info);
    }
    //保存再次编辑
    public function preservation_again_edit()
    {
        $data = $this->data;
        $id = $this->getUserID($data['token']);
        $save = array(
            'username'=>$data['username'],
            'license_name'=>$data['license_name'],
            'license_id'=>$data['license_id'],
            'license_img'=>$data['license_img'],
            'phone'=>$data['phone'],
            'number'=>$data['number'],
            'license_address'=>$data['license_address'],
            'delivery_time'=>$data['delivery_time'],
            'password'=>$data['password'],
//            'other'=>$data['other'],
            'status'=>1
        );

        try{
            $code = 2;
            $msg = 'token无效或已过期';
            if($id){
                foreach ($save as $key=>$item) {
                    if(empty($item)){
                        $code = 2;
                        $msg = '所需参数不能为空';
                        $this->ApiReturn($code,$msg,'');
                    }
                }
                $password = M('member')->where('id = %d',$id)->getField('password');
                if ($save['password'] == $password) {
                    M('member')->where('id = %d',$id)->setField($save);
                } else {
                    $save['password'] = md5($save['password']);
                    M('member')->where('id = %d',$id)->setField($save);
                }
                $code = 1;
                $msg = '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = "系统错误";
        }

        $this->ApiReturn($code,$msg);
    }

    //撤销申请
    public function revoke_application()
    {
        $data = $this->data;
        $id = $this->getUserID($data['token']);
        try{
            $code = 2;
            $msg = 'token无效或已过期';
            if($id){
                $info = M('member')->where('id = %d',$id)->find();
                @unlink('.'.$info['license_img']);
                @unlink('.'.$info['avatar']);
                M('member')->where('id = %d',$id)->delete();
                $code = 1;
                $msg .= '成功';
            }
        }catch(\Exception $e){
            $code = -1;
            $msg = "系统错误";
        }
        $this->ApiReturn($code,$msg);
    }

//    public function wx()
//    {
//        Vendor('Weixin.Wx');
//        $wx = new \Wx();
//        $wx->AppID="wxdc9e14679e197743";
//        $wx->AppSecret='261799ab3fd83b82f1d1c1d53c4bb8cd';
//        if (!isset($_GET['code'])){
//            //触发微信返回code码
//            $baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
//            $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$wx->AppID."&redirect_uri=".$baseUrl."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
//            Header("Location: $url");
//            exit();
//        } else {
//            //获取code码，以获取openid
//            $code = $_GET['code'];
//            $data = $data=$wx->getWebUser($code);
//            print_r($data);
//        }
//    }







}